#include "includes.h"
#include "CEnemy.h"

INT CEnemy::_Speed[4] = { 1, 1, 1, 1 };

CEnemy::CEnemy() {

	_Timer.SetTime(1000);
	_TimerMove.SetTime(20);
}

CEnemy::~CEnemy()
{
}

VOID CEnemy::Logic() {

	_Bullet.Logic();

	if (_Timer.IsTimeOut()) {
		INT idx = rand() % 4;
		_TankDir = g_dir[idx];
	}

	if (rand() % 100 > 50) {
		Shoot();
	}

	if (_Run && _TimerMove.IsTimeOut()) {
		_Pos.X += DirMove[_TankDir][0] * _Speed[0];
		_Pos.Y += DirMove[_TankDir][1] * _Speed[0];
	}

	if (_Pos.X < 0)  _Pos.X = 0;
	if (_Pos.X > _WindowWidth)  _Pos.X = _WindowWidth;
	if (_Pos.Y < 0) _Pos.Y = 0;
	if (_Pos.Y > _WindowHeight)_Pos.Y = _WindowHeight;

	_Frame = _Run ? _Frame + 1 : 0;
}

VOID CEnemy::Draw(Gdiplus::Graphics& graphics) {

	_Bullet.Draw(graphics);

	auto image = CTank::GetCTank(_TankKind, _lv, _TankDir, _Frame);
	graphics.DrawImage(image, _Pos.X, _Pos.Y, Box32, Box32);
}

VOID CEnemy::Set(TankDir tankDir, BOOL Run) {

	_Timer.Start();

	_TimerMove.Start();

	_Pos = { 0, 0 };

	_TankDir = tankDir;

	_Run = Run;

	_lv = 0;

	_Frame = 0;

	_hp = 0;

	_TankKind = TankKind_gray;
	_EnemyKind = EnemyKind_general;
}

VOID CEnemy::Set(INT X, INT Y, TankDir tankDir) {
	_Pos = { X, Y };
	_TankDir = tankDir;
	_Run = FALSE;
}

VOID CEnemy::Shoot() {
	_Bullet.Set(_Pos.X, _Pos.Y, _TankDir, 12);
}
